package com.huluobo.a2202mvvm_im_douyindemo.login;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.huluobo.a2202mvvm_im_douyindemo.bean.LoginBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseViewModel;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.net.Const;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/4/19.
 */
public class LoginViewModel extends BaseViewModel<LoginRepo> {

    MutableLiveData<LoginUserBean> loginEaseSuccess = new MutableLiveData<>();
    MutableLiveData<String> loginEaseFailed = new MutableLiveData<>();

    MutableLiveData<LoginBeanResult> loginSuccess = new MutableLiveData<>();

    MutableLiveData<String> loginFailed = new MutableLiveData<>();

    public void login(String username, String password) {
        //封装参数,把属性封装成一个body
        //1.生成map,保存键值对
        Map<String, String> map = new HashMap<>();
        map.put(Const.PARAM_USERNAME, username);
        map.put(Const.PARAM_PASSWORD, password);

        //把map转换为一个json
        String json = new Gson().toJson(map);
        //把json转换为body,因为接口中只接收body
        RequestBody body = RequestBody.create(MediaType.parse(Const.MEDIA_TYPE), json);
        repo.login(body, loginSuccess, loginFailed);
    }

    public void loginEase(String username, String password) {
        repo.loginEase(username, password, loginEaseSuccess, loginEaseFailed);
    }

    @Override
    protected LoginRepo createRepo() {
        return new LoginRepo();
    }
}
